package chapter06

/**
  * create by undeRdoG on  2021-06-02  11:46
  * 凡心所向，素履以往，生如逆旅，一苇以航。
  */
object Test12_Singleton {

  def main(args: Array[String]): Unit = {
    val s1: Student12 = Student12.getInstanceWithHungry()
    val s2: Student12 = Student12.getInstanceWithHungry()

    println(s1)
    println(s2)

    val s3: Student12 = Student12.getInstanceWithlazy()
    println(s3)


  }
}


class Student12 private(val name: String, val age: Int) {
  def printInfo(): Unit = {

    println(s"student name ${name}  age ${age}  school ${Student11.school}")
  }
}


object Student12{

  /**
  *   饿汉模式
  * */
  private val student: Student12 = new Student12("alice",12)
  def getInstanceWithHungry():Student12 ={
    student
  }


  private var s : Student12 = _

  def getInstanceWithlazy(): Student12 = {
    if (s != null){
      s
    }else{
      s = new Student12("peter",88)
      s
    }
  }
}